package com.xxchat.controller;

import com.xxchat.annotation.GlobalInterceptor;
import com.xxchat.entity.constants.QueryConstants;
import com.xxchat.entity.constants.SysConstants;
import com.xxchat.entity.dto.SysSettingDTO;
import com.xxchat.entity.enums.ResponseCodeEnum;
import com.xxchat.entity.enums.SysVersionStatusEnum;
import com.xxchat.entity.po.SysVersion;
import com.xxchat.entity.query.SysVersionQuery;
import com.xxchat.entity.vo.ResponseVO;
import com.xxchat.exception.BusinessException;
import com.xxchat.service.SysVersionService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 * @author Waicc
 */
@RestController("adminSysVersionController")
@RequestMapping("/admin")
public class AdminSysVersionController extends ABaseController {
    @Resource
    private SysVersionService sysVersionService;

    @RequestMapping("/loadVersionList")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO loadVersionList() {
        // TODO 日期、版本号等查询
        SysVersionQuery query = new SysVersionQuery();
        query.setOrderBy(QueryConstants.ORDER_BY_ID_DESC);
        return getSuccessResponseVO(this.sysVersionService.findListByPage(query));
    }

    @RequestMapping("/saveVersion")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO saveVersion(Integer id,
                                  @NotEmpty @Pattern(regexp = SysConstants.REGEX_VERSION, message = SysConstants.MESSAGE_VERSION) String version,
                                  String updateDesc,
                                  @NotNull Integer fileType,
                                  String outerLink,
                                  MultipartFile file) {
        SysVersion sysVersion = new SysVersion();
        sysVersion.setId(id);
        sysVersion.setVersion(version);
        sysVersion.setUpdateDesc(updateDesc);
        sysVersion.setFileType(fileType);
        sysVersion.setOuterLink(outerLink);
        this.sysVersionService.saveVersion(sysVersion, file);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/delVersion")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO delVersion(@NotNull Integer id) {
        SysVersion version = this.sysVersionService.getSysVersionById(id);
        if (null == version || !SysVersionStatusEnum.INIT.getStatus().equals(version.getStatus())) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        this.sysVersionService.deleteSysVersionById(id);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/postVersion")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO postVersion(@NotNull Integer id, @NotNull Integer status, String grayScaleUid) {
        this.sysVersionService.postVersion(id, status, grayScaleUid);
        return getSuccessResponseVO(null);
    }
}
